﻿using System;
using System.Collections;
using BP.DA;
using BP.En;
using BP.Difference;
using BP;
using BP.Web;
using Aliyun.OSS;
namespace BP.Sys
{
    /// <summary>
    /// 自动任务
    /// </summary>
    public class AutoTask : EntityMyPK
    {
        public override UAC HisUAC
        {
            get
            {
                UAC uac = new UAC();
                uac.Readonly();
                return uac;
            }
        }

        #region 基本属性
        public string TaskName
        {
            get
            {
                return this.GetValStringByKey("TaskName");
            }
        }
        public string TaskModel
        {
            get
            {
                return this.GetValStringByKey("TaskModel");
            }
        }
        public string Docs
        {
            get
            {
                return this.GetValStringByKey("Docs");
            }
        }
        public string Times
        {
            get
            {
                return this.GetValStringByKey("Times");
            }
        }
        public string DBSrc
        {
            get
            {
                return this.GetValStringByKey("DBSrc");
            }
        }
        #endregion

        #region 构造方法
        /// <summary>
        /// 自动任务
        /// </summary>
        public AutoTask()
        {
        }

        /// <summary>
        /// EnMap
        /// </summary>
        public override Map EnMap
        {
            get
            {
                if (this._enMap != null)
                    return this._enMap;
                Map map = new Map("Sys_AutoTask", "自动任务");

                map.AddMyPK();
                map.AddTBString("TaskName", null, "名称", true, true, 0, 200, 200);
                map.AddTBString("TaskModel", null, "内容类型", true, true, 0, 200, 100);
                map.AddTBStringDoc("Docs", null, "执行内容", true, true, true);
                map.AddTBString("Times", null, "执行时间", true, true, 0, 200, 300);
                map.AddTBString("DBSrc", null, "数据源", true, true, 0, 200, 50);
                map.AddTBStringDoc("NoteMark", null, "说明", true, true, true);
                map.AddTBAtParas(500);

                this._enMap = map;
                return this._enMap;
            }
        }
        #endregion

        public string RunTask()
        {
            if (this.TaskModel.Equals("SQL") == true)
            {
                string sql = this.Docs;
                sql = sql.Replace("@WebUser.No", WebUser.No);
                sql = sql.Replace("@WebUser.Name", WebUser.Name);
                sql = sql.Replace("@WebUser.DeptNo", WebUser.DeptNo);
                sql = sql.Replace("@WebUser.OrgNo", WebUser.OrgNo);
                if (DataType.IsNullOrEmpty(this.DBSrc) == true || this.DBSrc.Equals("DBSrc"))
                {
                    this.SetValByKey("DBSrc", "local");
                }
                SFDBSrc sFDBSrc = new SFDBSrc();
                int i = sFDBSrc.RunSQL(sql);
                return "执行成功:影响" + i + "条.";
            }

            if (this.TaskModel.Equals("BU") == true)
            {

                BuessUnitBase enBuesss = BP.Sys.Base.Glo.GetBuessUnitEntityByEnName(this.Docs);
                string doc = enBuesss.DoIt();
                return doc;
            }

            //@hongyan
            if (this.TaskModel.Equals("Method") == true)
            {
                Method rm = BP.En.ClassFactory.GetMethod(this.Docs);
                rm.Init();
                object obj = rm.Do();
                return obj.ToString();
            }

            return "没有判断的类型:"+this.TaskModel;
        }

        #region 重写
        public override Entities GetNewEntities
        {
            get { return new AutoTasks(); }
        }
        #endregion 重写
    }
    /// <summary>
    /// 自动任务s
    /// </summary>
    public class AutoTasks : EntitiesMyPK
    {
        #region 构造
        public AutoTasks()
        {
        }
        #endregion

        #region 重写
        /// <summary>
        /// 得到它的 Entity
        /// </summary>
        public override Entity GetNewEntity
        {
            get
            {
                return new AutoTask();
            }
        }
        #endregion

        #region 为了适应自动翻译成java的需要,把实体转换成List.
        /// <summary>
        /// 转化成list
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.List<AutoTask> Tolist()
        {
            System.Collections.Generic.List<AutoTask> list = new System.Collections.Generic.List<AutoTask>();
            for (int i = 0; i < this.Count; i++)
            {
                list.Add((AutoTask)this[i]);
            }
            return list;
        }
        #endregion 为了适应自动翻译成java的需要,把实体转换成List.

        #region 为了适应自动翻译成java的需要,把实体转换成IList, c#代码调用会出错误。
        /// <summary>
        /// 转化成list
        /// </summary>
        /// <returns>List</returns>
        public System.Collections.Generic.IList<AutoTask> ToJavaList()
        {
            return (System.Collections.Generic.IList<AutoTask>)this;
        }
        #endregion 为了适应自动翻译成java的需要,把实体转换成IList, c#代码调用会出错误。

    }
}
